package services;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import models.Repair;
import models.Repair;
import models.mappers.RepairMapper;
import models.mappers.RepairMapper;
import vos.RepairSeachVo;
import vos.RepairVo;
import vos.RepairSeachVo;

/**
 * 设备检修操作类
 * 
 * @ClassName: RepairService 
 * @author: wangsm
 * @date: 2016年5月31日 上午10:18:57
 */
public class RepairService {
	/**
	 * 统计检修
	 *
	 * @Title: countRepair 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countRepair(RepairSeachVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RepairMapper mapper = ss.getMapper(RepairMapper.class);
            return mapper.countRepair();
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取检修列表
	 *
	 * @Title: listRepair 
	 * @param vo
	 * @return
	 * @return: List<RegionVo>
	 */
	public static List<Repair> listRepair(RepairSeachVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RepairMapper mapper = ss.getMapper(RepairMapper.class);
            return mapper.listRepair(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchDel 
	 * @param ids
	 * @return
	 * @return: boolean
	 */
	public static boolean betchDel(String ids) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			RepairMapper mapper = ss.getMapper(RepairMapper.class);
            mapper.betchDel(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 删除城市等级
	 *
	 * @Title: delRepair 
	 * @param id 城市等级id
	 * @return
	 * @return: boolean
	 */
	public static boolean delRepair(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RepairMapper mapper = ss.getMapper(RepairMapper.class);
	        mapper.delRepair(id);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		
		return true;
	}
	/**
	 * 根据id获取城市等级信息
	 *
	 * @Title: getById 
	 * @param levelid 城市等级id
	 * @return
	 * @return: Repair
	 */
	public static RepairVo getById(Long levelid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			RepairMapper mapper = ss.getMapper(RepairMapper.class);
			return mapper.getById(levelid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存城市等级信息
	 *
	 * @Title: saveRepair 
	 * @param device 城市等级对象
	 * @return
	 * @return: boolean
	 */
	public static boolean saveRepair(Repair device) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RepairMapper mapper = ss.getMapper(RepairMapper.class);
			Date date=new Date();
			device.createtime=date;
	        mapper.saveRepair(device);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新城市等级信息
	 *
	 * @Title: updateRepair 
	 * @param device
	 * @return
	 * @return: boolean
	 */
	public static boolean updateRepair(Repair device) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RepairMapper mapper = ss.getMapper(RepairMapper.class);
			Date date=new Date();
			device.modifytime=date;
	        mapper.updateRepair(device);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 导出excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<Repair>
	 */
	public static List<RepairVo> exportXls(RepairSeachVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RepairMapper mapper = ss.getMapper(RepairMapper.class);
            return mapper.exportXls(vo);
        } finally {
            ss.close();
        }
	}
}
